using System;
using System.Threading;

using Microsoft.SPOT;

using mfRCF.Core;
using mfRCF.Core.Extensions;

using mfXmlSerializer;

using mfLogger;

using OST.Shared.SharedTypes;
using OST.Persistence;
using OST.Shared;
using OST.Calc.Tracking;
using OST.Motor;
using OST.Commands.Extensions;

namespace OST.Commands
{
    /// <summary>
    /// calibrates the motor
    /// </summary>
    public class MotorCalibrateGETCommand : mfRcfCommand
    {
        #region CTOR

        public MotorCalibrateGETCommand(string httpPath)
            : base(httpPath, new mfGETMethod())
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// wrapper-property 4 logging
        /// </summary>
        private ILoggerAppender Logger
        {
            get
            {
                return mfLogger.mfLogger.Instance;
            }
        }

        #endregion

        #region mfRcfCommand
        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            if (!MotorProvider.Instance.HasInstance)
            {
                this.WriteHttpResponseError(httpContext, new OSTException("no motor-instance available"));
                return;
            }

            if (!MotorProvider.Instance.MotorInstance.IsInitialized)
            {
                this.WriteHttpResponseError(httpContext, new OSTException("motor was never initialized"));
                return;
            }

            if (!ConfigurationProvider.Instance.HasConfig)
                throw new OSTException("no config available");

            if (TrackerProvider.Instance == null
                || TrackerProvider.Instance != null && !TrackerProvider.Instance.HasInstance)
                throw new OSTException("no tracker instance found");

            if (TrackerProvider.Instance.TrackerInstance.IsStarted)
            {
                this.WriteHttpResponseError(httpContext, new OSTException("tracker started, no calibration possible"));
                return;
            }

            var motorConfig = ConfigurationProvider.Instance.ConfigInstance.Motor;
            var motor = MotorProvider.Instance.MotorInstance;
            try
            {
                Logger.Info(GetType() + " executing, " + Debug.GC(true) + " bytes free");

                motor.Calibrate();

                //string msg = string.Empty;
                //if (motor.GetType() == typeof(EndStopSensorMotor))
                //    msg = "calibration-result:" + motor.MotorConfig.MoveDuration;
                //else if (motor.GetType() == typeof(HallSensorMotor))
                //    msg = "calibration-result:" + motor.MotorConfig.Interrupts;
                //else
                //    throw new OSTException("unknown motor-implementation: " + motor.GetType().Name);

                var msg = "calibration-result:" + motor.MotorConfig.Duration;
                this.WriteHttpResponseOK(httpContext, msg);

            }
            catch (Exception ex)
            {
                this.WriteHttpResponseError(httpContext, ex);
            }
        }

        #endregion
    }
}
